﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using SelfService.Model.Models;


namespace SelfService.ViewModel.SysDoctorVMs
{
    public partial class SysDoctorVM : BaseCRUDVM<SysDoctor>
    {
        [Display(Name = "工作时段")]
        public List<string> SelectedWorkTimeIntervalsIDs { get; set; }

        public SysDoctorVM()
        {
            SetInclude(x => x.Room);
            SetInclude(x => x.WorkTimeIntervals);
        }

        protected override void InitVM()
        {
            SelectedWorkTimeIntervalsIDs = Entity.WorkTimeIntervals?.Select(x => x.TimeIntervalId.ToString()).ToList();
        }

        public override void DoAdd()
        {
            Entity.WorkTimeIntervals = new List<DoctorWorkTimeInterval>();
            if (SelectedWorkTimeIntervalsIDs != null)
            {
                foreach (var id in SelectedWorkTimeIntervalsIDs)
                {
                     DoctorWorkTimeInterval middle = new DoctorWorkTimeInterval();
                    middle.SetPropertyValue("TimeIntervalId", id);
                    Entity.WorkTimeIntervals.Add(middle);
                }
            }
           
            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            Entity.WorkTimeIntervals = new List<DoctorWorkTimeInterval>();
            if(SelectedWorkTimeIntervalsIDs != null )
            {
                 foreach (var item in SelectedWorkTimeIntervalsIDs)
                {
                    DoctorWorkTimeInterval middle = new DoctorWorkTimeInterval();
                    middle.SetPropertyValue("TimeIntervalId", item);
                    Entity.WorkTimeIntervals.Add(middle);
                }
            }

            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
